package com.mrli.cat.weather.activity;

import android.app.Fragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mrli.cat.weather.R;
import com.mrli.cat.weather.database.WeatherContract;
import com.mrli.cat.weather.tools.Utility;

public class DetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
    private View mRootView;
    private Bundle mBundle;
    private static final int LOADER_WEATHER = 0x78;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getLoaderManager().initLoader(LOADER_WEATHER, null, this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_detail, null);
        mBundle = getActivity().getIntent().getExtras();
        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) mRootView.findViewById(R.id.awesome_toolbar);
        toolbar.setTitle("详细信息");
        toolbar.inflateMenu(R.menu.menu_detail);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_settings: {
                        Intent intent = new Intent(getActivity(), WeatherPreferenceActivity.class);
                        startActivity(intent);
                    }
                    return true;
                }
                return false;
            }
        });
        return mRootView;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        switch (id) {
            case LOADER_WEATHER: {
                return new CursorLoader(getActivity(),
                        WeatherContract.WEATHER_CONTENT_URI,
                        null,
                        WeatherContract.WeatherEntry._ID + "=?",
                        new String[]{String.valueOf(mBundle.getInt(WeatherContract.WeatherEntry._ID))},
                        null);
            }
            default:
                return null;
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (data.moveToFirst()) {
            //标题
            ((TextView) mRootView.findViewById(R.id.list_item_title_textview)).setText(
                    Utility.convertTimeStampToDetailActivityWeek(data.getLong(data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_TIME)))
            );
            //预报时间
            ((TextView) mRootView.findViewById(R.id.list_item_date_textview)).setText(
                    Utility.convertTimeStampToDetailActivityDate(data.getLong(data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_TIME))));
            //最低气温和最高气温
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
            String value = sharedPreferences.getString(WeatherPreferenceActivity.METRIC_KEY, "0");
            Boolean convert;
            if (value.equals("0")) {
                convert = false;
            } else {
                convert = true;
            }
            int highTemp = data.getInt(data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_TEMP_MAX));
            ((TextView) mRootView.findViewById(R.id.list_item_high_textview)).setText(Utility.convertCelsiusToFahrenheit(highTemp, convert));
            int lowTemp = data.getInt(data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_TEMP_MIN));
            ((TextView) mRootView.findViewById(R.id.list_item_low_textview)).setText(Utility.convertCelsiusToFahrenheit(lowTemp, convert));
            //气象图标
            int weatherId = data.getInt(data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_WEATHER));
            ((ImageView) mRootView.findViewById(R.id.list_item_icon)).setImageResource(Utility.convertIDtoWeatherArtIcon(weatherId));
            //气象描述
            ((TextView) mRootView.findViewById(R.id.list_item_desc)).setText(Utility.convertIDtoDes(weatherId));
            //湿度
            int hum = data.getInt(data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_HUMI));
            ((TextView) mRootView.findViewById(R.id.list_item_humidity)).setText("湿度:" + hum + "%");
            //风速
            int speed = data.getInt(data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_WIND_SPEED));
            int direction = data.getInt(data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_WIND_DEG));
            ((TextView) mRootView.findViewById(R.id.list_item_wind_desc)).setText("风速: " + speed + "km/h");
            int kpa = data.getInt(data.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_PRESSURE));
            //大气压
            ((TextView) mRootView.findViewById(R.id.list_item_pressure)).setText("大气压: " + kpa + "千帕");
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}
